<?php

class FundService
{
    /**
     * @var FundService
     */
    private static $instance;

    /**
     * Get the instance of FundService.
     *
     * @return FundService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new FundService();
        }
        return self::$instance;
    }

    public function __construct()
    {

    }

    /**
     * 触发基金成长挑战
     * @param $userId
     * @param $fundId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function triggerFund($userId, $fundId, $reqTime)
    {
        $status = 1;

        $isTrigger = $this->checkTrigger($userId, $fundId, $reqTime);
        if ($isTrigger != 0) {
            return array(
                'code' => $status
            );
        }

        //基金购买持续时间
        $continueTimeConf = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'fundItemTime', true)['value'];
        $timeArr = explode(':', trim(trim($continueTimeConf, '['), ']'));
        if ($timeArr[0] == $fundId) {
            $continueTime = $timeArr[1];
        }

        if (GameConst::FUND_LIMITED_TIME) {
            $endTime = $reqTime + $continueTime;
        } else {
            // 不限时 2099-12-30 00:00:00
            $endTime = 4102243200;
        }

        /**
         * @var FundInfoVoList $fundInfoList
         * @var FundInfoVo $fundInfo
         */
        $fundInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_FUND_INFO);
        $fundInfo = $fundInfoList->getElement($fundId, false);
        if (!$fundInfo) {
            $status = 0;
            $fundInfo = new FundInfoVo($userId, $fundId, $status, 0, [], $reqTime, 0, $endTime, true);
            $fundInfoList->addElement($fundInfo);
            $fundInfoList->setElement($fundId, $fundInfo);
        } else {
            $status = 0;
            if ($fundInfo->getEndTime() != $endTime) {
                $fundInfo->setEndTime($endTime);
            }
        }

        SzPersister::get()->setVoList($fundInfoList);
        return array(
            'code' => $status,
            'fundInfo' => $fundInfo->toArray()
        );
    }


    public function fundReward($userId, $fundId, $fundRewardId, $reqTime)
    {

        /**
         * @var FundInfoVoList $fundInfoList
         * @var FundInfoVo $fundInfo
         */
        $fundInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_FUND_INFO);
        $fundInfo = $fundInfoList->getElement($fundId, false);

        if (!$fundInfo) {
            return array(
                'code' => 999,//未触发
            );
        }

        if ($fundInfo->getStatus() != GameConst::FUND_BUY_STATUS) {
            return array(
                'code' => 998,//未支付
            );
        }

        $fundRewardConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_FUND_REWARD, $fundRewardId, true);

        //获取玩家等级
        $userLevel = UserService::get()->getUserLevel($userId);
        $receiveLv = $fundRewardConf['receiveLv'];
        if ($receiveLv > $userLevel) {
            return array(
                'code' => 997,//玩家等级未达到
            );
        }


        $reward = $fundRewardConf['itemReward'];
        //获取奖励
        $output = '';
        foreach ($reward as $entityId => $count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true, true);
            $output .= $entityId . ':' . abs($count) . '|';

        }

        $receivedSituationArr = json_decode($fundInfo->getRawReceivedSituation());

        $receivedSituationArr[] = $fundRewardId;
        $fundInfo->setReceivedSituation(json_encode($receivedSituationArr));
        $fundInfo->setUpdateTime($reqTime);
        $fundInfoList->updateElement($fundId, $fundInfo);

        SzPersister::get()->setVoList($fundInfoList);

        return array(
            'code' => 0,
        );
    }


    /**
     * 获取基金成长的配置
     * @return array
     */
    private function getFundConf($fundId)
    {
        $fundConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_DIAMOND_VALUE, $fundId, true);
        return $fundConf;
    }

    /**
     * @param $userId
     * @param $fundId
     * @param $reqTime
     * @return int
     */
    private function checkTrigger($userId, $fundId, $reqTime)
    {
        $result = 0;
        $fundConf = $this->getFundConf($fundId);

        if (!$fundConf) {
            $result = 1; //缺少配置
        }

        return $result;
    }


}
